package com.zhuo.project.order.service.task;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhuo.common.constant.RedisKeyConstants;
import com.zhuo.framework.config.K3Config;
import com.zhuo.framework.redis.RedisCache;
import com.zhuo.project.order.dto.k3.AccessResult;
import com.zhuo.project.order.dto.k3.ReturnInfo;
import com.zhuo.project.order.dto.k3.SendDeliveryInfo;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;

/**
 * @project name: ruoyi
 * @author: fuhao <fuhao@kuaishou.com>
 * @team 快手-商业化-直营电商
 * @Package com.zhuo.project.order.service.task
 * @create: 2020-04-11 14:50
 * @description:
 **/
@Service
public class K3Service {
    private static Logger logger = LoggerFactory.getLogger(K3Service.class);

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private K3Config k3Config;
    // test
    // private final  String k2Url = "http://116.62.138.246:3883/k3cloud/ERPGW.asmx/";

    public String signIn() throws Exception{
        if(redisCache.hasKey(RedisKeyConstants.K3_TOKEN_KEY)){
            String token = redisCache.getCacheObject(RedisKeyConstants.K3_TOKEN_KEY);
            return token;
        }
        String signUrl = k3Config.getUrl() + "Signin";
        logger.info("请求地址为：{}",signUrl);
        HashMap<String, Object> paramp = new HashMap<>();
        paramp.put("userName", k3Config.getUserName());
        paramp.put("pwd", k3Config.getPwd());
        paramp.put("number", k3Config.getNumber());
        String ret = HttpUtil.post(signUrl, paramp);
       logger.info("k3登陆返回信息为：{}",ret);
        AccessResult accessResult = JSONUtil.toBean(ret, AccessResult.class);
        String token = "-1";
        if (accessResult.getCode().equals("0")) {
            token = accessResult.getAccessToken();
        }else {
            logger.error(accessResult.getCode() + "  k3登陆错误信息为：" + accessResult.getMessage());
        }
        redisCache.setCacheObject(RedisKeyConstants.K3_TOKEN_KEY, token, 15, TimeUnit.MINUTES);
        return token;
    }

    public  AccessResult saveMisDelivery(SendDeliveryInfo sendDeliveryInfo) {
        String deliveryUrl = k3Config.getUrl() + "SaveMisDelivery";
        logger.info("发药请求地址为：{}",deliveryUrl);
        logger.info("发药请求参数：{}",JSONUtil.toJsonStr(sendDeliveryInfo));
        HashMap<String, Object> paramp = new HashMap<>();
        paramp.put("param",JSONUtil.toJsonStr(sendDeliveryInfo));
        String ret = HttpUtil.post(deliveryUrl, paramp);
        logger.info("k3 发药推送返回结果:{}", ret);
        AccessResult accessResult = JSONUtil.toBean(ret, AccessResult.class);
        if (!accessResult.getCode().equals("0")) {
            logger.error(accessResult.getCode() + "  k3推送发药错误信息为：" + accessResult.getMessage());
        }
        return accessResult;
    }

    public  AccessResult pushMisDeliveryT(ReturnInfo returnInfo) {
        String returnUrl =  k3Config.getUrl() + "PushMisDeliveryT";
        logger.info("退药请求地址为：{}",returnUrl);
        logger.info("发药请求参数：{}",JSONUtil.toJsonStr(returnInfo));
        HashMap<String, Object> paramp = new HashMap<>();
        paramp.put("param",JSONUtil.toJsonStr(returnInfo));
        String ret = HttpUtil.post(returnUrl, paramp);
        logger.info("k3 退药推送返回结果:{}", ret);
        AccessResult accessResult = JSONUtil.toBean(ret, AccessResult.class);
        if (!accessResult.getCode().equals("0")) {
            logger.error(accessResult.getCode() + "  k3推送退药错误信息为：" + accessResult.getMessage());
        }
        return accessResult;
    }
}
